package com.wo.cloud.auth.controller;

import com.wo.cloud.auth.service.impl.ClientDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/3/7 21:38
 * Description:
 */

@RestController
public class TestController {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private ClientDetailsServiceImpl service;

    @GetMapping("/getUser")
    public Principal getUser(Principal principal) {
        return principal;
    }

    @GetMapping("/getUserInfo")
    public Authentication getUserInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        // 获取token
//        Object details = SecurityContextHolder.getContext().getAuthentication().getDetails();
//        OAuth2AuthenticationDetails details1 = (OAuth2AuthenticationDetails) details;
        return authentication;
    }

    @GetMapping("/sysAdd")
    // 角色校验, @PreAuthorize 可以将登录用户的 roles/permissions 参数传到方法中
    @PreAuthorize("hasAnyRole({'system', 'test'})")
//    @PreAuthorize("hasAnyAuthority({'sys:add', 'test:query'})")
    public String getSysAdd() {
        return "sys:add || test:query info";
    }

    @GetMapping("/sysDel")
    // hasAnyRole
    @PreAuthorize("hasAuthority('sys:del')")
    public String getSysDel() {
        return "sys:del info";
    }

    @GetMapping("/testQuery")
    @PreAuthorize("hasAuthority('test:query')")
    public String getTestQuery() {
        return "test:query info";
    }

//    @PostMapping("/client")
//    public SysClientDetails save(@RequestBody SysClientDetails sysClientDetails) {
//        return service.save(sysClientDetails);
//    }

}
